package g4p.examples;

import g4p_controls.G4P;
import g4p_controls.GEvent;
import g4p_controls.GStick;
import processing.core.PApplet;

public class G4P_Stick extends PApplet {

	GStick joystick;
	int facing = -1;
	int dirX, dirY;
	float px, py;
	float speed = 0.8f;
	
	public void setup(){
		size(300,300);
		float ss = 80;
		joystick = new GStick(this, width-ss, height-ss, ss, ss);
		joystick.setMode(G4P.X8);
		strokeWeight(1.5f);
		px = width/2;
		py = height/2;
	}

	public void draw(){
		background(200,255,200);
		px = (px + dirX * speed + width) % width;
		py = (py + dirY * speed + height) % height;

		pushMatrix();
		translate(px, py);
		if(facing > 0)
			rotate(facing * PI/4);
		fill(200,200,255);
		stroke(32, 32, 160);
		beginShape();
		vertex(-20, -10);
		vertex(-20, 10);
		vertex(30, 0);
		endShape(CLOSE);
		fill(32, 32, 160);
		noStroke();
		ellipse(-6,0,10,6);
		popMatrix();
		fill(0);
		text(""+round(px)+"   "+round(py),30,30);
	}

	public void handleStickEvents(GStick stick, GEvent event) { 
		if(joystick == stick){
			facing = stick.getPosition();
			dirX = stick.getStickX();
			dirY = stick.getStickY();
		}
	}

}
